<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>确认付款</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/style.css" />
		<link rel="stylesheet" href="../css/iconfont.css" />
		<link rel="stylesheet" href="../css/wallet.css" />
	</head>

	<body>
		<div class="mui-content qrfuk_content">
			<div class="fuk_conbox">
				<div class="fuk_ctop">
					<div class="fuk_ctimg"><img id='store_thum'></div>
					<div class="fuk_spnam"></div>
				</div>
				<div class="fuk_center">
					<div>付款金额</div>
					<div class="fuk_pribox">
						<label>¥</label>
						<input id='pay_money' style="text-align: left;" onkeyup="clearNoNum(this)" type="text"></input>
					</div>
					<!--<div class="fuk_jif">
	    			<p>您本次可以使用鼓励金抵消费额度</p>
	    			<p class="fuk_jifnum"><em>80</em>鼓励金</p>
	    		</div>-->
				</div>
				<!--<p class="liuy">
					<a href="#">添加留言</a>
				</p>-->
				<button type="button" class="qrfuk_btn" id='qrfuk_btn'>确认付款</button>
			</div>
			<div class="fuk_sm" style="position: relative; margin-top: 80px;">©由龙信天下提供技术支持</div>
		</div>
		<input type="hidden" id='order_sn'>
		<input type="hidden" id='give_score'>
		<div id="callback"></div>
		<div id="cut_back"></div>
	</body>

</html>
<script src="../js/mui.min.js"></script>
<script src="../js/zepto.min.js"></script>
<script type="text/javascript" src="../js/date.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/layer/layer.min.js"></script>
<script type="text/javascript" src="../js/app.js"></script>
<script>
	$(function() {
		//记录扫码客户端，微信，或支付宝
		var pay_type = 0;
		var ua = window.navigator.userAgent.toLowerCase();
		var auth_code = getQueryString("auth_code");
		var shou_user_id = getUrlParam("fromuid");
		var member ="";
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			pay_type = 2;
		} else if(ua.match(/AlipayClient/i) == 'alipayclient') {
			pay_type = 3;
			if(auth_code == null) {
				var fromurl = location.href;
				var url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016030801195057&scope=auth_base&redirect_uri='+encodeURIComponent(fromurl);
				location.href = url;
			}
		}
				
		//确认付款
		$("#qrfuk_btn").click(function() {
			$("#qrfuk_btn").attr("disabled", true);
			if(pay_type == 3){
				//获取支付宝key
				alipay_user(auth_code,getUrlParam("fromuid"));
			}else if(pay_type == 2){
				//获取openid
				var openid = localStorage.getItem('openid');
				//获取用户信息
				member_by_openid(openid,getUrlParam("fromuid"));
			}
			var member = JSON.parse(localStorage.getItem('member'));
			
			//订单编号
			$('#order_sn').val(getSn());
			
			//付款金额
			var pay_money = $("#pay_money").val();
			if(pay_money == "") {
					layer.msg('请输入支付金额');
					$("#qrfuk_btn").attr("disabled", false);
					return false;
			}
			var act = "alipay";
			var jstype = "html";
			if(pay_type == 2) {
				act = "wechat";
				jstype = "jsonp";
			}
			$.ajax({
				url: APP.api_url + 'Payment/' + act,
				data: {
					WIDout_trade_no	: $("#order_sn").val(),
					WIDsubject		: "二维码付款",
					WIDtotal_fee	: pay_money,
					//WIDtotal_fee	: 0.01,
					WIDshow_url		: 'www',
					WIDbody			: "二维码付款",
					uid				: getUid(),
					order_ids		: shou_user_id,
					type			: "uesr_payee",
					user_type		: 1,
					token			: get_token(),
					openid			: member['openid'],
					pay_type		: pay_type
				},
				type: "post",
				dataType: jstype,
				jsonp: 'callback',
				jsonpCallback: 'jsonpCallback',
				success: function(data) {
					if(pay_type == 2) {
						callpay(data);
					} else {
						$("#callback").append(data);
					}
				}
			})
		});
	});
	
//只能数字和小数点
	function clearNoNum(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
	//调用微信JS api 支付
	function jsApiCall(jsApiParameters) {
		WeixinJSBridge.invoke('getBrandWCPayRequest', jsApiParameters,
			function(res) {
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					layer.msg('付款成功！', {
						  time: 1000
						}, function(){
							location.href="fuk-success.html?sn="+$("#order_sn").val();
						});
				} else {
					layer.msg('付款失败');
					$("#qrfuk_btn").attr("disabled",false);
				}
			});
	}

	function callpay(jsApiParameters) {
		console.log(jsApiParameters);
		if(typeof WeixinJSBridge == "undefined") {
			if(document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall(jsApiParameters), false);
			} else if(document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall(jsApiParameters));
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall(jsApiParameters));
			}
		} else {
			jsApiCall(jsApiParameters);
		}
	}
	function jsonpCallback(data) {
		$("#cut_back").append(data);
	}
</script>